package com.config;

/**
 * @author HuangSuip
 */


import java.io.IOException;
import com.job.ApplicationJob;
import net.sf.jabb.quartz.AutowiringSpringBeanJobFactory;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;


/*@Configuration
@EnableScheduling
@ContextConfiguration
@WebAppConfiguration
@ComponentScan(basePackages = {"com"}, excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Configuration.class)})
@MapperScan("com.xxx.cars.persistence")
@EnableTransactionManagement
@EnableAutoConfiguration*/
public class WebAppConfig {

/**
 * 配置拦截器
 *
 * @param registry
 * @author jodie
 *//*

//	public void addInterceptors(InterceptorRegistry registry) {
//		registry.addInterceptor(new UserSecurityInterceptor()).addPathPatterns(
//				"*/

    /**
     * 创建触发器工厂
     *
     * @param jobClass
     * @return
     * @author LDX
     */

    private static JobDetailFactoryBean createJobDetail(Class jobClass) {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(jobClass);
        factoryBean.setDurability(true);
        return factoryBean;
    }

    /**
     * 创建定时器工厂
     *
     * @param jobDetail
     * @param pollFrequencyMs
     * @return
     * @author LDX
     */

    private static CronTriggerFactoryBean createTrigger(JobDetail jobDetail, long pollFrequencyMs) {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(jobDetail);
        factoryBean.setStartDelay(0L);
        factoryBean.setCronExpression("0/5 * * * * ? ");//每5秒执行一次
        return factoryBean;
    }

/**加载quartz数据源配置,quartz集群时用到
 * @return
 * @author LDX
 * @throws IOException
 */

//	    @Bean
//	    public Properties quartzProperties() throws IOException {
//	        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
//	        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
//	        propertiesFactoryBean.afterPropertiesSet();
//	        return propertiesFactoryBean.getObject();
//	    }

    /**
     * ");
     * //	}
     */
    @Bean
    public JobFactory jobFactory(ApplicationContext applicationContext) {
        AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
        jobFactory.setApplicationContext(applicationContext);
        return jobFactory;
    }

    /**
     * 调度工厂bean
     *
     * @param jobFactory
     * @return
     * @throws IOException
     * @author LDX
     */

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(
            JobFactory jobFactory,
            @Qualifier("cronJobTrigger") Trigger cronJobTrigger
    ) throws IOException {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        // this allows to update triggers in DB when updating settings in config file:
        //用于quartz集群,QuartzScheduler 启动时更新己存在的Job，这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
        factory.setOverwriteExistingJobs(true);
        //用于quartz集群,加载quartz数据源
//	        factory.setDataSource(dataSource);
        factory.setJobFactory(jobFactory);
        //QuartzScheduler 延时启动，应用启动完20秒后 QuartzScheduler 再启动
        factory.setStartupDelay(20);
        //用于quartz集群,加载quartz数据源配置
//	        factory.setQuartzProperties(quartzProperties());
        //注册触发器
        factory.setTriggers(cronJobTrigger);

        return factory;
    }

    /**
     * 加载触发器
     *
     * @return
     * @author LDX
     */

    @Bean
    public JobDetailFactoryBean sampleJobDetail() {
        return createJobDetail(ApplicationJob.class);
    }

/**创建一个以频率为触发节点,以毫秒为单位,可以指定每隔x秒执行任务
 * @param jobDetail
 * @param pollFrequencyMs
 * @author LDX
 * @return private static SimpleTriggerFactoryBean createTrigger(JobDetail jobDetail, long pollFrequencyMs) {
SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
factoryBean.setJobDetail(jobDetail);
factoryBean.setStartDelay(0L);
factoryBean.setRepeatInterval(pollFrequencyMs);
factoryBean.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
// in case of misfire, ignore all missed triggers and continue :
factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT);
return factoryBean;
}*/

    /**
     * 加载定时器
     *
     * @param jobDetail
     * @param frequency
     * @return
     * @author LDX
     */

    @Bean(name = "cronJobTrigger")
    public CronTriggerFactoryBean sampleJobTrigger(
            @Qualifier("sampleJobDetail") JobDetail jobDetail,
            @Value("${samplejob.frequency}") long frequency
    ) {
        return createTrigger(jobDetail, frequency);
    }
}
